package com.zhongkun.datahome.service.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongkun.datahome.service.domain.Sku;
import com.zhongkun.datahome.service.mapper.SkuMapper;
import com.zhongkun.datahome.service.req.qry.sku.SkuQry;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Objects;

/**
 * @Author: lifengsheng
 * @Date: 2025/4/14 17:15
 * @Desc
 */
@Repository
public class SkuRepository extends ServiceImpl<SkuMapper, Sku> {
    public IPage<Sku> pageSku(SkuQry qry) {
        IPage<Sku> page = new Page<>(qry.getPageNo(), qry.getPageSize());

        return this.page(page, new LambdaQueryWrapper<Sku>()
                .like(StringUtils.isNotBlank(qry.getSkuName()), Sku::getSkuName, qry.getSkuName())
                .eq(StringUtils.isNotBlank(qry.getCategory()), Sku::getCategory, qry.getCategory())
                .eq(Objects.nonNull(qry.getStatus()), Sku::getStatus, qry.getStatus())
                .in(CollectionUtils.isNotEmpty(qry.getSkuIds()), Sku::getId, qry.getSkuIds())
                .orderByDesc(Sku::getId));
    }
}
